﻿using ApiServer.HostedService;
using Dapper;
using Domain.Entity.User;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Quartz;
using Quartz.Impl;
using System.Reflection;

namespace ApiServer.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [AllowAnonymous]
    public class ValuesController : ControllerBase
    {
        [HttpGet]
        public async Task<IActionResult> TestQuartz()
        {
            //创建一个调度器
            var scheduler = await new StdSchedulerFactory().GetScheduler();
            //启动
            scheduler.Start();
            //创建一个工作任务
            var job = JobBuilder.Create<MyJob>().Build();
            //创建一个触发器
            var trigger = TriggerBuilder.Create().WithCronSchedule("* * * * * ?").Build();
            //将这三个对象连一起
            await scheduler.ScheduleJob(job, trigger);
            return Ok();
        }
        [HttpGet("reflection")]
        public IActionResult TestReflection(string typeName)
        {
            //获取出这个程序集所有的类型名称，与typename相匹配，
            Type t = Assembly.Load("Application.DTO").GetTypes().Where(m => m.Name.Contains(typeName)).FirstOrDefault();

            //判断是否为空
            if (t == null)
            {
                return BadRequest();
            }

            //查询
            var list = t.GetConstructors().Select(m => m.Name);//.Select(m=>m.Name).ToList();
            //foreach (var property in list)
            //{
            //    Console.WriteLine($"");
            //}
            return Ok(list);
        }
        /// <summary>
        /// 用户反射显示ORM简单示例
        /// </summary>
        /// <param name="typeName">model类型</param>
        /// <returns></returns>
        [HttpGet("reflection2")]
        public IActionResult TestReflection2(string typeName)
        {
            //获取该类
            var t = Assembly.Load("Domain.Entity").GetTypes().Where(m => m.Name == typeName).FirstOrDefault();

            if (t == null)
            {
                return BadRequest();
            }

            //获取所有的属性
            var props = t.GetProperties().ToList();

            //拼接SQL
            string sql = "insert into " + t.Name + " values (";
            foreach (var item in props)
            {
                sql += item.Name + ",";
            }
            sql = sql.TrimEnd(',');
            sql += ")";

            return Ok(sql);
        }
        /// <summary>
        /// 用户表反射生成SQL语句
        /// </summary>
        /// <param name="typeName">model类型</param>
        /// <returns></returns>
        [HttpPost("reflection3")]
        public IActionResult TestReflection3(UserInfo user)
        {
            //获取类型
            Type t = user.GetType();

            //获取待添加的类的属性名称
            var props = t.GetProperties();

            string sql = $"insert into {t.Name} values (";
            //拼接SQL
            foreach (var item in props)
            {
                sql += item.GetValue(user).ToString() + ",";
            }
            sql = sql.TrimEnd(',');
            sql += ")";
            return Ok(sql);
        }

        //dapper实现用户列表显示

        private const string connectstring = "Data Source=.;Initial Catalog=RBACDB;Integrated Security=True";
        [HttpGet("show")]
        public IActionResult Show()
        {
            using (var conn = new SqlConnection(connectstring))
            {
                string sql = "select RoleUsers.UserId,RoleUsers.RoleId from UserInfo join RoleUsers on UserInfo.Id=RoleUsers.UserId";
                List<dynamic> list = conn.Query<dynamic>(sql).ToList();
                //string sql1 = "select * from UserInfo";

                return Ok(list);
            }
        }
    }
}
